package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @program: gmall-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-05 09:47
 **/
@Service
public class ItemServiceImpl implements ItemService {

    //  远程调用service-product-client
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;


    //根据skuId  查询sku商品详细信息
    @Override
    public Map<String, Object> getBySkuId(Long skuId) {

        // 创建返回数据
        HashMap<String, Object> map = new HashMap<>();

        //skuInfo sku的基本属性
        CompletableFuture<SkuInfo> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = this.productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);

            return skuInfo;
        }, threadPoolExecutor);

        //categoryView 平台属性值详情
        CompletableFuture<Void> categoryViewFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = this.productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        }, threadPoolExecutor);

        //skuAttrList 平台属性
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> attrList = this.productFeignClient.getAttrList(skuId);

            List<HashMap<String, Object>> skuAttrList = attrList.stream().map(baseAttrInfo -> {
                HashMap<String, Object> mp = new HashMap<>();
                mp.put("attrName", baseAttrInfo.getAttrName());
                mp.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());

                return mp;
            }).collect(Collectors.toList());
            map.put("skuAttrList", skuAttrList);
        }, threadPoolExecutor);

        //price 当前最新的价格
        CompletableFuture<Void> priceFuture = CompletableFuture.runAsync(() -> {
            BigDecimal price = this.productFeignClient.getSkuPrice(skuId);
            map.put("price", price);
        }, threadPoolExecutor);

        //spuPosterList 海报
        CompletableFuture<Void> spuPosterListFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            List<SpuPoster> spuPosterBySpuId = this.productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId());
            map.put("spuPosterList", spuPosterBySpuId);
        }, threadPoolExecutor);

        //spuSaleAttrList 销售属性
        CompletableFuture<Void> spuSaleAttrListFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = this.productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        }, threadPoolExecutor);

        //valuesSkuJson 检索是哪个sku 的字符串
        CompletableFuture<Void> valuesSkuJsonFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = this.productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String s = JSONObject.toJSONString(skuValueIdsMap);
            map.put("valuesSkuJson", s);
        }, threadPoolExecutor);

        // 进行热度增加
        CompletableFuture<Void> listFuture = CompletableFuture.runAsync(() -> {
            this.listFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);

        // 等待所有的线程执行完之后进行统一返回
        CompletableFuture.allOf(
                skuInfoFuture,
                categoryViewFuture,
                voidCompletableFuture,
                priceFuture,
                spuPosterListFuture,
                spuSaleAttrListFuture,
                valuesSkuJsonFuture,
                listFuture).join();

        return map;

        // 默认null
    }
}
